package managedbean;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import util.localization.MenuLocalization;

/**
 *
 * @author Amelia
 */
@ManagedBean
@RequestScoped
public class StakeholderTemplateManagedBean implements Serializable {

    private MenuLocalization menuLocalization;
    private MenuModel microPortalModel;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private transient FacesContext context;
    private transient UIViewRoot view;

    public StakeholderTemplateManagedBean() {
        microPortalModel = new DefaultMenuModel();
        menuLocalization = new MenuLocalization();
        String url = getRequestURL();
        if (url.contains("Portal") || url.contains("Branch") || url.contains("Media")) {
            microPortalModel = generateMicroPortalMenu();
        } else if (url.contains("Advertisement")) {
            microPortalModel = generateAdvMenu();
        } else if (url.contains("Account")) {
            microPortalModel = generateAccountMenu();
        } else if (url.contains("Event")) {
            microPortalModel = generateEventMenu();
        } else {
            microPortalModel = generateMailBoxMenu();
        }
    }

    public void initView(PhaseEvent event) {
        microPortalModel = new DefaultMenuModel();
        menuLocalization = new MenuLocalization();
        String url = getRequestURL();
        if (url.contains("Portal") || url.contains("Branch") || url.contains("Media")) {
            microPortalModel = generateMicroPortalMenu();
        } else if (url.contains("Advertisement")) {
            microPortalModel = generateAdvMenu();
        } else if (url.contains("Account")) {
            microPortalModel = generateAccountMenu();
        } else if (url.contains("Event")) {
            microPortalModel = generateEventMenu();
        } else {
            microPortalModel = generateMailBoxMenu();
        }
    }

    private String getRequestURL() {
        Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request instanceof HttpServletRequest) {
            return ((HttpServletRequest) request).getRequestURL().toString();
        } else {
            return "";
        }
    }

    private void initContextMemebers() {
        context = FacesContext.getCurrentInstance();
        view = context.getViewRoot();
    }
    //Generate MicroPortalMenu

    private MenuModel generateMicroPortalMenu() {
        initContextMemebers();
        MenuModel model = new DefaultMenuModel();

        if (!ctx.getExternalContext().getSessionMap().isEmpty()) {
            String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
            int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
            String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

            //---------Branch Portion-------
            Submenu submenu = new Submenu();
            submenu.setLabel(menuLocalization.getStakeholderMenuItem("BRANCH_MANAGEMENT"));
            submenu.setId(view.createUniqueId());
            MenuItem menu1 = new MenuItem();
            menu1.setId(view.createUniqueId());
            menu1.setValue(menuLocalization.getStakeholderMenuItem("ADD_BRANCH"));
            String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddBranch.xhtml";
            menu1.setUrl(URL);
            submenu.getChildren().add(menu1);
            model.addSubmenu(submenu);

            MenuItem menu2 = new MenuItem();
            menu2.setId(view.createUniqueId());
            menu2.setValue(menuLocalization.getStakeholderMenuItem("VIEW_BRANCH"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/ViewAllBranch.xhtml";
            menu2.setUrl(URL);
            submenu.getChildren().add(menu2);
            model.addSubmenu(submenu);

            //---------Media Portion-------
            submenu = new Submenu();
            submenu.setLabel(menuLocalization.getStakeholderMenuItem("PHOTO_GALLERY"));

            MenuItem menu3 = new MenuItem();
            menu3.setId(view.createUniqueId());
            menu3.setValue(menuLocalization.getStakeholderMenuItem("ADD_PHOTOS"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddMedia.xhtml";
            menu3.setUrl(URL);
            submenu.getChildren().add(menu3);
            model.addSubmenu(submenu);

            MenuItem menu4 = new MenuItem();
            menu4.setValue(menuLocalization.getStakeholderMenuItem("VIEW_PHOTOS"));
            menu4.setId(view.createUniqueId());
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/ViewMedia.xhtml";
            menu4.setUrl(URL);
            submenu.getChildren().add(menu4);
            model.addSubmenu(submenu);

            MenuItem menu5 = new MenuItem();
            menu5.setId(view.createUniqueId());
            menu5.setValue(menuLocalization.getStakeholderMenuItem("DELETE_PHOTOS"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/RemoveMedia.xhtml";
            menu5.setUrl(URL);
            submenu.getChildren().add(menu5);
            model.addSubmenu(submenu);
        } else {
            ctx.getExternalContext().getRealPath("index.xhtml");
        }
        return model;
    }

    //Create Account Management Menu
    private MenuModel generateAccountMenu() {
         initContextMemebers();
        MenuModel model = new DefaultMenuModel();

        Submenu submenu = new Submenu();
        submenu.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("MY_ACCOUNT_DETAILS"));
        model.addSubmenu(submenu);

        return model;
    }

    //Create Advertisement Menu
    private MenuModel generateAdvMenu() {
         initContextMemebers();
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("ADVERTISEMENT_MANAGEMENT"));

        MenuItem menu6 = new MenuItem();
        menu6.setId(view.createUniqueId());
        menu6.setValue(menuLocalization.getStakeholderMenuItem("ADD_ADVERTISEMENT"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddAdvertisement.xhtml";
        menu6.setUrl(URL);
        submenu.getChildren().add(menu6);
        model.addSubmenu(submenu);
        return model;
    }

    //Create Advertisement Menu
    private MenuModel generateMailBoxMenu() {
         initContextMemebers();
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("MY_MAIL_BOX"));

        MenuItem menu7 = new MenuItem();
        menu7.setId(view.createUniqueId());
        menu7.setValue(menuLocalization.getStakeholderMenuItem("COMPOSE_MAIL"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/ComposeMessage.xhtml";
        menu7.setUrl(URL);
        submenu.getChildren().add(menu7);
        model.addSubmenu(submenu);

        MenuItem menu8 = new MenuItem();
        menu8.setId(view.createUniqueId());
        menu8.setValue(menuLocalization.getStakeholderMenuItem("INBOX"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/Myinbox.xhtml";
        menu8.setUrl(URL);
        submenu.getChildren().add(menu8);
        model.addSubmenu(submenu);

        MenuItem menu9 = new MenuItem();
        menu9.setId(view.createUniqueId());
        menu9.setValue(menuLocalization.getStakeholderMenuItem("OUTBOX"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/Myoutbox.xhtml";
        menu9.setUrl(URL);
        submenu.getChildren().add(menu9);
        model.addSubmenu(submenu);

        MenuItem menu10 = new MenuItem();
        menu10.setValue(menuLocalization.getStakeholderMenuItem("TRASH"));
        menu10.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/ViewTrash.xhtml";
        menu10.setUrl(URL);
        submenu.getChildren().add(menu10);
        model.addSubmenu(submenu);

        return model;
    }

    //Create Events Menu
    private MenuModel generateEventMenu() {
         initContextMemebers();
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("EVENT_MANAGEMENT"));
        submenu.setId(view.createUniqueId());

        MenuItem menu11 = new MenuItem();
        menu11.setId(view.createUniqueId());
        menu11.setValue(menuLocalization.getStakeholderMenuItem("ADD_EVENT"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/AddEvent.xhtml";
        menu11.setUrl(URL);
        submenu.getChildren().add(menu11);
        model.addSubmenu(submenu);

        /*
        MenuItem menu8 = new MenuItem();
        menu8.setValue(menuLocalization.getStakeholderMenuItem("MANAGE_EVENT"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/ManageEvent.xhtml";
        menu8.setUrl(URL);
        submenu.getChildren().add(menu8);
        model.addSubmenu(submenu);
         */

        MenuItem menu12 = new MenuItem();
        menu12.setId(view.createUniqueId());
        menu12.setValue(menuLocalization.getStakeholderMenuItem("VIEW_ALL_EVENT"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/ViewAllEvent.xhtml";
        menu12.setUrl(URL);
        submenu.getChildren().add(menu12);
        model.addSubmenu(submenu);

        return model;
    }

    public MenuModel getMicroPortalModel() {
        return microPortalModel;
    }

    public void setMicroPortalModel(MenuModel microPortalModel) {
        this.microPortalModel = microPortalModel;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public MenuLocalization getMenuLocalization() {
        return menuLocalization;
    }

    public void setMenuLocalization(MenuLocalization menuLocalization) {
        this.menuLocalization = menuLocalization;
    }
}